#region (c) 2014 THN Solutions LLC. - All rights reserved
/*
THN TELEPORT version 2

Copyright (c) 2014, THN Solutions LLC. ( www.ThnSolutions.com )
Author: Nguyễn, M. Hải                 ( www.Minh-Hai.com     )
All rights reserved.

This library is dual-licensed.
  + For commercial software, please obtain a commercial license from THN Solutions LLC.

  + For free software, this library is licensed under GPL version 3. A summary of GPLv3 is
    listed below. You should also find a copy of GPLv3 as file License_GPLv3.txt included
	with the source-code files.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License version 3 as published by
    the Free Software Foundation  of the License.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#endregion

#region Using directives
using System;
using System.Reflection;
using Thn;
#endregion

namespace Thn.Communication.Teleporting
{
    /// <summary>
    /// Error raised by teleporting layer
    /// </summary>
    public class TeleportException : Thn.Application.ExceptionBase
    {
        #region Get Message
        /// <summary>
        /// Empty message builder
        /// </summary>
        static string GetMessage()
        {
            string result = "";
            if (Thn.Application.Language.Language.Current == Thn.Application.Language.Vietnamese.Vietnamese.Instance)
                result = "Lỗi trong quá trình teleport dữ liệu";
            else
                result = "Error occured during teleportation";
            return result;            
        }

        /// <summary>
        /// translate message
        /// </summary>
        static string GetMessage_NoGateway(string gateway)
        {
            string result = "";
            if (Thn.Application.Language.Language.Current == Thn.Application.Language.Vietnamese.Vietnamese.Instance)
                result = string.Format("Không tìm thấy gateway [{0}] ", gateway);
            else
                result = string.Format("Could not find gateway [{0}] ", gateway);
            return result;
        }
        #endregion

        #region Build
        /// <summary>
        /// Build exception
        /// </summary>
        /// <param name="sender">the subsystem that raises this error</param>
        /// <param name="control">the control that is not supported</param>
        public static TeleportException Build_FromServer(TeleportExceptionInfo info)
        {
            TeleportException result = new TeleportException(info);
            result.Source = "SERVER";            

            return result;
        }
        #endregion

        #region Publish
        /// <summary>
        /// Publish exception
        /// </summary>
        /// <param name="sender">the subsystem that raises this error</param>
        /// <param name="control">the control that is not supported</param>
        [System.Diagnostics.DebuggerHidden]
        public static void Publish_FromServer(TeleportExceptionInfo info)
        {
            TeleportException error = Build_FromServer(info);
            //write error to log
            if (Thn.Communication.Teleporting.TeleportingExceptionBase.LogError)
            {
                Log.Error(error);
                Log.Debug(info.StackTrace);
            }

            //throw error
            if (!Thn.Communication.Teleporting.TeleportingExceptionBase.SilentError) throw error;
        }

        /// <summary>
        /// Publish exception
        /// </summary>
        /// <param name="sender">the subsystem that raises this error</param>
        /// <param name="control">the control that is not supported</param>
        [System.Diagnostics.DebuggerHidden]
        public static void Publish_FromClient(TeleportExceptionInfo info)
        {
            TeleportException error = new TeleportException(info);
            error.Source = "CLIENT";
            //write error to log
            if (Thn.Communication.Teleporting.TeleportingExceptionBase.LogError)
            {
                Log.Error(error);
                Log.Debug(info.StackTrace);
            }

            //throw error
            if (!Thn.Communication.Teleporting.TeleportingExceptionBase.SilentError) throw error;
        }

        /// <summary>
        /// Publish exception No Gateway
        /// </summary>
        [System.Diagnostics.DebuggerHidden]
        public static void Publish_NoGateway(string gateway)
        {
            TeleportException error = new TeleportException(gateway);

            //write error to log
            if (Thn.Communication.Teleporting.TeleportingExceptionBase.LogError) Log.Error(error);

            //throw error
            if (!Thn.Communication.Teleporting.TeleportingExceptionBase.SilentError) throw error;
        }
        #endregion

        #region Constructors
        /// <summary>
        /// Empty constructor
        /// </summary>
        public TeleportException()
            : base(GetMessage()) { }

        /// <summary>
        /// Create a new instance
        /// </summary>
        public TeleportException(string message, Exception inner)
            : base(message, inner) { }

        /// <summary>
        /// Full constructor
        /// </summary>
        /// <param name="gateway">the gateway that could not be found</param>
        public TeleportException(string gateway)
            : base(GetMessage_NoGateway(gateway)) { }

        /// <summary>
        /// Create a new instance based on an info object
        /// </summary>
        public TeleportException(TeleportExceptionInfo info)
            : base(info.BuildMessage())
        {

        }
        #endregion
    }
}
